<?php
namespace app\admin\controller;

use app\common\model\AppleRepayPlan;
use app\common\model\NokiaMember;
use app\common\model\OppoOrder;
use think\admin\Controller;

class Plan extends Controller{

    public $plan_status_arr = ['待归还', '已结清', '已逾期', '已作废'];

    /**
     * 还款计划
     * @auth true
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(){
        $this->title = '还款计划';
        $date = date('Y-m-d');
        $this->plan_date = $this->get['plan_date'] ?? $date . ' - ' . $date;
        $query = AppleRepayPlan::mQuery();
        $user = $this->app->session->get('user');
        if($user['id'] != 10000) $query->where('merchant_id', '=', $user['id']);
        $query->equal('status');
        $query->equal('have_overdue');
        $query->equal('refund_sts');
        $query->equal('order_id');
        $query->equal('plan_id');
        $query->like('order_id,plan_id,shop_name');
        if($this->plan_date){
            $dateArr = explode(' - ', $this->plan_date);
            $query->whereBetween('plan_date', $dateArr);
        }
        $query->where(['del_flag' => 0]);
        $query->order('plan_date asc')->page();
    }

    public function _index_page_filter(array &$data){
        foreach ($data as &$vo){
            $member = NokiaMember::mk()->where(['member_id' => $vo['member_id']])->find();
            $vo['name'] = $member['real_name'];
            $vo['mobile'] = $member['mobile'];
            $vo['status_str'] = $this->plan_status_arr[$vo['status']];
            $vo['buy_amount'] = OppoOrder::mk()->where(['order_id' => $vo['order_id']])->value('buy_amount');
        }
    }
}